<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\IcenClass;
use App\Admin\Forms\TimeClass;
use App\Admin\Repositories\User;
use App\Models\Team;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Forms\Basketball;
use Illuminate\Support\Facades\DB;
use function foo\func;

class UserController extends AdminController
{
    public function index(Content $content)
    {
        return $content->title('学员')
            ->description('列表')
            ->body($this->grid());
    }

    public function create(Content $content)
    {
        return $content->title('球队')
            ->description('加入')
            ->body($this->form());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()->orderByDesc('update_time');
            $grid->fixColumns(2, -1);
            $grid->column('user_name','真实姓名')->display(function ($value){
                if ($this->user_level_status == 1){
                    return $value.'(会员身份过期)';
                }
                return $value;
            })->editable(true);
            $grid->column('user_nickname','昵称');
            $grid->column('user_headimgurl','微信图像')->image(config('alioss.ossServer'), 50, 50);
            $grid->column('user_phone','手机号');
            $grid->column('user_sign','签名');


            $grid->level('用户身份')->using([0 => '会员', 1 => '学员',2=>'家长'])->dot(
                [
                    0 => 'primary',
                    1 => 'danger',
                    2 => 'success',
                ],
                'primary' // 默认颜色
            );
            $grid->column('period','剩余课时')->editable(true);
            $grid->column('class_end_time','合同结束时间')->display(function ($value){
                return  $value ? date('Y-m-d',$value) : '空';
            });
            $grid->column('team_id','球队名称')->display(function ($value){
                if ($value){
                    return Team::getTeamById($value);
                }else{
                    if ($this->level ==1){
                        return  Modal::make()
                            ->title('加入球队')
                            ->body(Basketball::make()->payload(['id' => $this->id]))
                            ->button('加入球队');
                    }else{
                        return '无';
                    }
                    }

            });

            $grid->column('leastatus','休课状态')->display(function ($value){
                return $value ? '已经休课' : '暂未休课';
            });
            $grid->column('课时增加')->display(function ($value){
                  return  Modal::make()
                     ->title('增加课时')
                     ->body(IcenClass::make()->payload(['id' => $this->id]))
                     ->button('增加课时');
            });
            $grid->column('合同修改')->display(function ($value){
                  return  Modal::make()
                     ->title('时间修改')
                     ->body(TimeClass::make()->payload(['id' => $this->id]))
                     ->button('时间修改');
            });
            $grid->tools('<a href="User/create" class="btn btn-primary disable-outline">加入球队</a>');


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('level', '身份选择', [0=>'会员', 1=>'学员', 2=>'家长']);
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
            //禁用新增按钮
            $grid->disableCreateButton();
                $grid->filter(function (Grid\Filter $filter) {
                    // 更改为 panel 布局
                    $filter->panel();
                    $filter->equal('user_phone', '手机号')->width(3);
                    $filter->like('user_name', '真实姓名')->width(3);
                    $filter->where('team_id', function ($query) {
                        $query->whereHas('team', function ($query) {
                            $query->where('team_name', 'like', "%{$this->input}%");
                        });
                    }, '球队名称')->width(3);
                });
        });

    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->select('team_id','球队')->options('team/teamData');
            $form->multipleSelect('user_id','球员')->options('user/notPlayer');

            if ($form->isEditing()){
                $form->text('user_name');
                $form->text('period');
            }
            $form->saving(function (Form $form){
                if ($form->isCreating()){
                    $team_id = $form->team_id;
                    if (!$team_id){
                        return $form->error('球队不能为空');
                    }
                    if (!$form->user_id[0]){
                        return $form->error('球员不能为空');
                    }
                    $new_user = [];
                    foreach ($form->user_id as $k => $v) {
                        if ($v) {
                            $new_user[] = $v;
                        }
                    }
                    $res = DB::table('user')->whereIn('id', $new_user)->update(['team_id' => $team_id, 'update_time' => time()]);
                    if ($res) {
                        return $form->redirect('User', '保存成功');
                    } else {
                        return $form->error('数据保存失败');
                    }
                }
            });
        });
    }
    public function ownerDate(){
            $OwnerData = \App\Models\User::where(['level'=>1,'team_id'=>0])->get(['id','user_name as text'])->toArray();
        return $OwnerData;
    }
    public function player(){
            $OwnerData = \App\Models\User::where('level',1)->get(['id','user_name as text'])->toArray();
        return $OwnerData;
    }

    public function notPlayer(){
        $OwnerData = \App\Models\User::where('level',1)->where('team_id',0)->get(['id','user_name as text'])->toArray();
        return $OwnerData ?? '';
    }

}
